package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Product;
import com.qf.service.ProductService;
import com.qf.service.Impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;



@WebServlet("/productFindByIdController.action")
public class ProductFindByIdController extends HttpServlet {

    private ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        //判断
        if(id != null){
            //查询单个商品
            Product product =  productService.findById(id);
            //判断
            if(product !=null){
                //返回给前端
                map.put("data",product);
                map.put("state",200);

                //返回数据给前端
                resp.getWriter().write(JSON.toJSONString(map));
            }
        }else {
            throw new RuntimeException("id不能为空!");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
